package com.enlorenz.core.channel.socket.imp.longconnection;

import java.util.ArrayList;
import java.util.List;

import com.enlorenz.common.util.TimeUtils;
import com.enlorenz.core.base.IReqResMessageHandle;
import com.enlorenz.core.channel.socket.common.reader.INioReader;
import com.enlorenz.core.channel.socket.common.reader.ISocketReader;
import com.enlorenz.core.channel.socket.common.translate.IMessageBytesTransformer;
import com.enlorenz.core.channel.socket.common.writer.INioWriter;
import com.enlorenz.core.channel.socket.common.writer.ISocketWriter;
import com.enlorenz.core.engine.AbstractEngine;
import com.enlorenz.core.engine.IEngine;
import com.enlorenz.core.engine.manager.EngineManager;
import com.enlorenz.core.queue.transfer.imp.WaitMessageQueue;

/**
 * socket先收后发的双通道长连接
 * 使用双通道模式，将收发进行分离，提高传输效率
 * @author lcb
 */
public class LongRecvSendDoubleChannel extends AbstractEngine{
	//客户端通道
	private LongClientSocketChannel clientChannel;
	//服端通道
	private NioLongServerSocketChannel serverChannel;
	//客户端IP
	private String clientIp;
	//客户端口
	private int clientPort;
	//客户端的数据转换器
	private IMessageBytesTransformer clientMessageBytesTransformer;
	//客户端队列
	private WaitMessageQueue clientQueue;
	//客户端写出者
	private ISocketWriter clientWriter;
	//客户端读取者
	private ISocketReader socketReader;
	//服务端口
	private int serverPort;
	//服务等待数量
	private int backlog=10;
	
	//服务端写出者
	private INioWriter serverWriter;
	//服务端读取者
	private INioReader serverReader;
	//服务端端的数据转换器
	private IMessageBytesTransformer serverMessageBytesTransformer;
	
	//接收消息处理器
	private IReqResMessageHandle recvMsgHandle;
	//系统消息处理器
	private IReqResMessageHandle linkMsgHandle;
	//处理接收线程的数量
	private int handRecvThreadNum=5;
	private EngineManager engineManager=new EngineManager();
	//检查线程休眠时间
	private long checkSleepTime=TimeUtils.SECOND_MILLI*5;
	
	@Override
	public boolean isAlive() {
		return true;
	}

	@Override
	protected void initEngine() throws Exception {
		clientChannel=new LongClientSocketChannel();
		clientChannel.setIp(clientIp);
		clientChannel.setPort(clientPort);
		clientChannel.setMessageBytesTransformer(clientMessageBytesTransformer);
		clientChannel.setSendQueue(clientQueue);
		clientChannel.setRecvQueue(null);
		clientChannel.setSocketWriter(clientWriter);
		clientChannel.setSocketReader(socketReader);
		clientChannel.init();
		
		serverChannel=new NioLongServerSocketChannel();
		serverChannel.setBacklog(backlog);
		serverChannel.setListenPort(serverPort);
		serverChannel.setNioReader(serverReader);
		serverChannel.setNioWriter(serverWriter);
		serverChannel.setRecvMsgHandle(recvMsgHandle);
		serverChannel.setHandRecvThreadNum(handRecvThreadNum);
		serverChannel.setMessageBytesTransformer(serverMessageBytesTransformer);
		serverChannel.setLinkMsgHandle(linkMsgHandle);
		serverChannel.init();
		
		List<IEngine> engines=new ArrayList<IEngine>();
		engines.add(clientChannel);
		engines.add(serverChannel);
		engineManager.setCheckThreadSleepTime(checkSleepTime);
		engineManager.setEngines(engines);
		engineManager.init();
		
		
	}
	
	
	@Override
	protected void startEngine() throws Exception {
		engineManager.startEngines();
	}

	@Override
	protected void shutdownEngine() throws Exception {
		engineManager.stopEngines();
	}


	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	public void setClientPort(int clientPort) {
		this.clientPort = clientPort;
	}

	public void setClientMessageBytesTransformer(
			IMessageBytesTransformer clientMessageBytesTransformer) {
		this.clientMessageBytesTransformer = clientMessageBytesTransformer;
	}

	public void setClientQueue(WaitMessageQueue clientQueue) {
		this.clientQueue = clientQueue;
	}

	public void setClientWriter(ISocketWriter clientWriter) {
		this.clientWriter = clientWriter;
	}

	public void setSocketReader(ISocketReader socketReader) {
		this.socketReader = socketReader;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	public void setBacklog(int backlog) {
		this.backlog = backlog;
	}

	public void setServerWriter(INioWriter serverWriter) {
		this.serverWriter = serverWriter;
	}

	public void setServerReader(INioReader serverReader) {
		this.serverReader = serverReader;
	}

	public void setServerMessageBytesTransformer(
			IMessageBytesTransformer serverMessageBytesTransformer) {
		this.serverMessageBytesTransformer = serverMessageBytesTransformer;
	}

	public void setRecvMsgHandle(IReqResMessageHandle recvMsgHandle) {
		this.recvMsgHandle = recvMsgHandle;
	}

	

	public void setLinkMsgHandle(IReqResMessageHandle linkMsgHandle) {
		this.linkMsgHandle = linkMsgHandle;
	}

	public void setHandRecvThreadNum(int handRecvThreadNum) {
		this.handRecvThreadNum = handRecvThreadNum;
	}
	
}
